package com.cskaoyan.service.wx.zjh;

import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.vo.WxUserIndexVo;
import com.cskaoyan.module.pojo.*;
import com.cskaoyan.module.regionvo.BrandListVo;
import com.cskaoyan.module.systemVO.CategoryListVO;
import com.cskaoyan.module.vo.wx.zjh.CatalogCurrentVO;
import com.cskaoyan.module.vo.wx.zjh.CatalogIndexVO;
import com.cskaoyan.module.vo.wx.zjh.UserIndexVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/11/14:03
 */
@Service
public class BrandAndCatalogServiceImpl implements BrandAndCatalogService {
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketOrderMapper orderMapper;

    BaseRespVo baseRespVo = new BaseRespVo();

    @Override
    public BaseRespVo catalogCurrent(Integer id) {

        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id);

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L2");
        criteria.andPidEqualTo(marketCategory.getId());

        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);

        CatalogCurrentVO catalogCurrentVO = new CatalogCurrentVO(marketCategory, marketCategories);
        baseRespVo.setData(catalogCurrentVO);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");

        return baseRespVo;
    }

    @Override
    public BaseRespVo catalogIndex() {

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);

        MarketCategory marketCategory = marketCategories.get(0);

        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample1.createCriteria();
        criteria1.andLevelEqualTo("L2");
        criteria1.andPidEqualTo(marketCategory.getId());

        List<MarketCategory> marketCategories2 = categoryMapper.selectByExample(marketCategoryExample1);

        CatalogIndexVO catalogIndexVO = new CatalogIndexVO(marketCategories, marketCategory, marketCategories2);

        baseRespVo.setData(catalogIndexVO);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo brandDetail(Integer id) {
        MarketBrand marketBrand = brandMapper.selectByPrimaryKey(id);
        baseRespVo.setData(marketBrand);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo userIndex(Integer userId) {
        //待付款
        Integer unpaid = orderMapper.selectCountStatus(userId,101);
        //待发货
        Integer unship = orderMapper.selectCountStatus(userId,201);
        //待收货
        Integer unrecv = orderMapper.selectCountStatus(userId,301);
        //待评价
        Integer uncomment = orderMapper.selectCountStatus(userId,401);

        UserIndexVO userIndexVO = new UserIndexVO(unpaid, unship, unrecv, uncomment);
        WxUserIndexVo wxUserIndexVo = new WxUserIndexVo();
        wxUserIndexVo.setOrder(userIndexVO);

        baseRespVo.setData(wxUserIndexVo);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo brandList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);

        MarketBrandExample example = new MarketBrandExample();

        List<MarketBrand> marketBrands = brandMapper.selectByExample(example);

        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);

        BrandListVo brandListVo = new BrandListVo((int) pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageSize(), pageInfo.getPageNum(), marketBrands);
        baseRespVo.setData(brandListVo);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }
}
